#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   For_Jenkins_2_test.py
@Time    :   2022/08/31 13:31:28
@Author  :   YangLiang 
@Version :   1.0
@Contact :   yangliang@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# here put the import lib
from pickle import FALSE, TRUE
from pytest_assume.plugin import assume
 
import pytest
import time
import requests
from sympy import I
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
from collections.abc import Iterable

from sqlalchemy import false
sys.dont_write_bytecode = True

import project_CAN
import Set_up


class Test_BYD_CheckCanType(object):

    @allure.severity('normal')
    @allure.title('Test check PCAN can type in default session with physical addressing')
    @allure.description('Test check PCAN can type in default session with physical addressing')
    def test_Check_VCan_CanEDl_phy(self, action, Driver):
        iStep = 1
        Driver['CAN'].checkReqResp(Driver, '10 01', '50 01', 0, iStep)

        Driver['CAN'].fush_q_buff()  # 清缓存  
        time.sleep(0.1)
        Driver['CAN'].send_Frame(Set_up.reqID,[0x02, 0x3E, 0X00])
        time.sleep(0.1)
        ALl_data,buffer_size = Driver['CAN'].read_frame()
        response_data = ALl_data[ALl_data['AIdentifier'] == Set_up.resID]
        respData = response_data.iloc[0]['ADatas'][:16]
        actuRespStr = binascii.b2a_hex(bytearray(respData), ' ') 
        logging.info('V_CAN responseID:[0x{0:X}], Data:[{1}]'.format(Set_up.resID, actuRespStr)) 
        if Set_up.CAN_Type == 'CAN':
            BRS = 0
            EDL = response_data.iloc[0]['AIsRemote']
            logging.info('V_CAN BRS:[{0:X}], EDL:[{0:X}]'.format(BRS, EDL)) 
            logging.info('check EDL == 0') 
            with assume: assert EDL == 0
        else:
            BRS = response_data.iloc[0]['FFDProperties'] & 0x02 >> 1
            EDL = response_data.iloc[0]['FFDProperties'] & 0x01
            logging.info('V_CAN BRS:[{0:X}], EDL:[{0:X}]'.format(BRS, EDL)) 

            logging.info('check EDL == 1') 
            with assume: assert EDL == 1   
    
    @allure.severity('normal')
    @allure.title('Test check PCAN can BRS in default session with physical addressing')
    @allure.description('Test check PCAN can BRS in default session with physical addressing')
    def test_Check_VCan_CanBRS_phy(self, action, Driver):
        iStep = 1
        Driver['CAN'].checkReqResp(Driver, '10 01', '50 01', 0, iStep)

        Driver['CAN'].fush_q_buff()  # 清缓存  
        time.sleep(0.1)
        Driver['CAN'].send_Frame(Set_up.reqID,[0x02, 0x3E, 0X00])
        time.sleep(0.1)
        ALl_data,buffer_size = Driver['CAN'].read_frame()
        response_data = ALl_data[ALl_data['AIdentifier'] == Set_up.resID]
        respData = response_data.iloc[0]['ADatas'][:16]
        actuRespStr = binascii.b2a_hex(bytearray(respData), ' ') 
        logging.info('V_CAN responseID:[0x{0:X}], Data:[{1}]'.format(Set_up.resID, actuRespStr)) 
        if Set_up.CAN_Type == 'CAN':
            BRS = 0
            EDL = response_data.iloc[0]['AIsRemote']
            logging.info('V_CAN BRS:[{0:X}], EDL:[{0:X}]'.format(BRS, EDL)) 
            logging.info('check BRS == 0') 
            with assume: assert BRS == 0
        else:
            BRS = response_data.iloc[0]['FFDProperties'] & 0x02 >> 1
            EDL = response_data.iloc[0]['FFDProperties'] & 0x01
            logging.info('V_CAN BRS:[{0:X}], EDL:[{0:X}]'.format(BRS, EDL)) 
            logging.info('check BRS == 1') 
            with assume: assert BRS == 1   

    @allure.severity('normal')
    @allure.title('Test check PCAN can type in default session with physical addressing')
    @allure.description('Test check PCAN can type in default session with physical addressing')
    def test_Check_PCan_CanEDl_phy(self, action, Driver):
        iStep = 1
        Driver['CAN'].checkReqResp(Driver, '10 01', '50 01', 0, iStep)

        BRS,EDL = self.read_BRS_EDL_from64E_80(Driver)

        logging.info('P_CAN BRS:[{0:X}], EDL:[{0:X}]'.format(BRS, EDL)) 
        logging.info('check EDL == 1') 
        with assume: assert EDL == 1
    
    @allure.severity('normal')
    @allure.title('Test check PCAN can BRS in default session with physical addressing')
    @allure.description('Test check PCAN can BRS in default session with physical addressing')
    def test_Check_PCan_CanBRS_phy(self, action, Driver):
        iStep = 1
        Driver['CAN'].checkReqResp(Driver, '10 01', '50 01', 0, iStep)

        BRS,EDL = self.read_BRS_EDL_from64E_80(Driver)

        logging.info('P_CAN BRS:[{0:X}], EDL:[{0:X}]'.format(BRS, EDL)) 

        logging.info('check BRS == 1') 
        with assume: assert BRS == 1

    
    def read_BRS_EDL_from64E_80(self, Driver):
        Driver['CAN2'].fush_q_buff()  # 清缓存  
        time.sleep(0.1)
        ALl_data,buffer_size = Driver['CAN2'].read_frame()
        try:
            response_data_64E = ALl_data[ALl_data['AIdentifier'] == 0x64E]
            respData_64E = response_data_64E.iloc[0]['ADatas'][:16]
            respData =  respData_64E
            actuRespStr = binascii.b2a_hex(bytearray(respData), ' ') 
            logging.info('P_CAN responseID:[0x{0:X}], Data:[{1}]'.format(0x64E, actuRespStr))
            BRS = response_data_64E.iloc[0]['FFDProperties'] & 0x02 >> 1
            EDL = response_data_64E.iloc[0]['FFDProperties'] & 0x01
        except:
            response_data_80 = ALl_data[ALl_data['AIdentifier'] == 0x80]
            respData_80 = response_data_80.iloc[0]['ADatas'][:16]
            respData =  respData_80
            actuRespStr = binascii.b2a_hex(bytearray(respData), ' ') 
            logging.info('P_CAN responseID:[0x{0:X}], Data:[{1}]'.format(0x80, actuRespStr)) 
            BRS = response_data_80.iloc[0]['FFDProperties'] & 0x02 >> 1
            EDL = response_data_80.iloc[0]['FFDProperties'] & 0x01

        return BRS, EDL